package org.example.helloworld.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.helloworld.entity.History;
import org.example.helloworld.entity.Notice;
import org.example.helloworld.entity.PrePay;
import org.example.helloworld.entity.Result;
import org.example.helloworld.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/history")
public class HistoryController {

    @Autowired
    private HistoryService historyService;
    @GetMapping
    public List<History> getAllRates() {
        return historyService.list();
    }

    @GetMapping("/{id}")
    public History getRateById(@PathVariable String id) {
        return historyService.getById(id);
    }

    @PostMapping
    public boolean addRate(@RequestBody History rate) {
        return historyService.save(rate);
    }

    @PutMapping
    public boolean updateRate(@RequestBody History rate) {
        return historyService.updateById(rate);
    }

    @DeleteMapping("/{id}")
    public boolean deleteRate(@PathVariable String id) {
        return historyService.removeById(id);
    }


    @PostMapping("/search")
    public List<History> searchRates(@RequestParam String preId) {
        QueryWrapper<History> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pre_id", preId);
        return historyService.list(queryWrapper);
    }

    @PostMapping("/noticeDelete")
    public Result noticeDelete(@RequestBody Map<String, List<Long>> request) {
        List<Long> ids = request.get("ids");
        if (ids == null || ids.isEmpty()) {
            return Result.error("未提供要删除的通知ID");
        }
        boolean isSuccess = historyService.removeByIds(ids);
        return isSuccess ? Result.success() : Result.error("删除失败");
    }
    @GetMapping("/list")
    public Result noticeList(
            @RequestParam int page,
            @RequestParam int size,
            @RequestParam(required = false) String search,
            @RequestParam Long userId) {
        Page<History> historyPage = new Page<>(page, size);
        IPage<History> resultPage = historyService.getHistoryList(historyPage, search, userId);
        return Result.success(resultPage);
    }


}
